package cn.gengar.types.design.framework.tree;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.util.Objects;

/**
 * 责任链路由
 * @param <T>
 * @param <D>
 * @param <R>
 */
public abstract class AbstractStrategyRouter<T, D, R> implements StrategyHandler<T, D, R>, StrategyMapper<T, D, R> {

    @Getter
    @Setter
    protected StrategyHandler<T, D, R> defaultStrategyHandler = StrategyHandler.DEFAULT;

    public R router(T requestParameter, D dynamicContext) throws Exception {
        StrategyHandler<T, D, R> strategyHandler = get(requestParameter, dynamicContext);
        if (Objects.nonNull(strategyHandler)) {
            return strategyHandler.apply(requestParameter, dynamicContext);
        }

        return defaultStrategyHandler.apply(requestParameter, dynamicContext);
    }
}
